module AutoBase
  module Viewable
    def self.included(receiver)
      receiver.class_eval do
        @@edges = {}
        def self.from(name, state = {}, &block)
          @@edges[[name, state[:to]]] = block
        end

        def to(name)
          g = AutoBase::Graph.new(@@edges.keys)
          start = g.vertices[@current_view_name]
          stop  = g.vertices[name]
          path = g.shortest_path(start, stop).map {|vertex| vertex.name}
          0.upto(path.size - 2) do |index|
            block = @@edges[[path[index], path[index+1]]]
            instance_eval(&block)
          end

          @current_view_name = name
        end
      end
    end
  end
end
